package com.dongqi.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;


public class XmlToPO {

	/**
	* xml文件配置转换为对对象
	* @param xmlPath  xml文件路径
	* @param load    java对象.Class
	* @return    java对象
	* @throws JAXBException
	* @throws IOException
	*/
    public static Object xmlToObject(String data,Class<?> load) throws JAXBException, IOException{
         JAXBContext context = JAXBContext.newInstance(load);
         Unmarshaller unmarshaller = context.createUnmarshaller();
         Object object = unmarshaller.unmarshal(new StringReader(data));
         return object;
    }

	public static String toData(Object object) throws Exception {
		JAXBContext jc = JAXBContext.newInstance(object.getClass());
		Marshaller m = jc.createMarshaller();
		StringWriter output = new StringWriter(2048);
		m.marshal(object, output);
		String data = output.toString();
		return data;
	}
//	public static void main(String[] args) throws JAXBException, IOException {
//		XmlToModel x = new XmlToModel();
//    	MsgReceResult result = new MsgReceResult();
//    	result.setBizNo("1234567890");
//    	result.setEtpsPreentNo("0987654321");
//    	try {
//			String sResult = x.toData(result);
//			System.err.println(sResult);
//			MsgReceResult object = (MsgReceResult)XmlToModel.xmlToObject(sResult,MsgReceResult.class);
//			System.err.println(object.getBizNo()+"::"+object.getEtpsPreentNo());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
